package com.hst.capacity.application.amqp;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.model.entity.order.pool.OrderPoolEntity;
import com.hst.capacity.domain.model.request.elong.order.ElongOrderParam;
import com.hst.capacity.domain.service.AliTripService;
import com.hst.capacity.domain.service.TcELongService;
import com.hst.capacity.domain.service.impl.TcELongServiceImpl;
import com.hst.capacity.infra.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 抢单消费者
 *
 * @author hans
 */
@Slf4j
@Component
@RabbitListener(queues = {QueueConstants.GRABBING_ORDER_LY})
public class GrabbingOrderReceiver {

    @Autowired
    TcELongService tcELongService;

    @Autowired
    AliTripService aliTripService;

//    @RabbitHandler
//    public void receive(String content) {
//        log.info("队列消息：{}", content);
//        // 这里写消息队列的消费逻辑
//        // ...
//
//        try {
//            ElongOrderParam param;
//            if (StringUtil.isNotBlank(content)) {
//                param = JSON.parseObject(content, ElongOrderParam.class);
//                tcELongService.virtualAcceptOrder(param);
//            }
//        } catch (Exception e) {
//            log.info("receive error:" + e.toString());
//        }
//
//    }


    @RabbitHandler
    public void receive(String content) {
        log.info("队列消息：{}", content);
        // 这里写消息队列的消费逻辑
        // ...

        try {
            JSONObject object = JSON.parseObject(content);
            log.info("JSONObject Object:" + object.toJSONString());
            if (object.containsKey(Constants.ThirdpartyKbn.TongchengRide)) {
                String value = object.getString(Constants.ThirdpartyKbn.TongchengRide);
                log.info("执行 JSONObject getString:" + value);
                ElongOrderParam param = JSON.parseObject(value, ElongOrderParam.class);
                if (param != null) {
                    log.info("执行 TcELongService:" + JSON.toJSONString(param));
                    tcELongService.virtualAcceptOrder(param);
                }
            } else if (object.containsKey(Constants.ThirdpartyKbn.AliTrip)) {
                String idOrder = object.getString(Constants.ThirdpartyKbn.AliTrip);
                if (StringUtil.isNotBlank(idOrder)) {
                    aliTripService.virtualAcceptOrder(idOrder);
                }
            }
        } catch (Exception e) {
            log.info("receive error:" + e.toString());
        }

    }

}
